package com.foreveredu.erlms.web.struts.action;

import java.util.List;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.foreveredu.erlms.domain.Rank;
import com.foreveredu.erlms.service.IExamService;
import com.foreveredu.erlms.web.struts.actionform.RankActionForm;


public class RankAction extends AbstractBaseDispatchAction {

	private IExamService examService = null;

	public IExamService getExamService() {
		return examService;
	}
	public void setExamService(IExamService examService) {
		this.examService = examService;
	}

	// 跳转到报考等级管理列表页面
	public ActionForward list(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		List<Rank> rankList = examService.findRanks();
		request.setAttribute("rankList", rankList);

		return mapping.findForward("rankList");
	}
	
	//修改报考等级是否开启状态
	public ActionForward updateRankFlag(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		RankActionForm rankf = (RankActionForm) form;
		Rank rank =examService.findRankById(rankf.getId());
		if(rank.getFlag()==1){
			rank.setFlag(0);
			examService.updateRank(rank);
			return mapping.findForward("updateRankFlag");
		}
		if(rank.getFlag()==0){
			rank.setFlag(1);
			examService.updateRank(rank);
			return mapping.findForward("updateRankFlag");
		}
		return mapping.findForward("updateRankFlag");
	}

	// 完成报考等级添加操作
	public ActionForward save(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		RankActionForm rankf = (RankActionForm) form;
		
		Rank rank = new Rank();
		PropertyUtils.copyProperties(rank, rankf);
		// 判断当前输入的报考等级是否合法！
		if (rankf.getCode() == null || "".equals(rankf.getCode())) {
			request.setAttribute("rank", rank);
			request.setAttribute("rankIdError", "请输入等级及语言代码！");
			return mapping.findForward("toRankList");
		} 
		else if (examService.findRankByCode(rankf.getCode()) != null) {
			request.setAttribute("rank", rank);
			request.setAttribute("rankIdRequest", "该等级及语言代码已存在，请重新输入！");

			return mapping.findForward("toRankList");
		}
		if(!Pattern.matches("^[0-9]+$", rankf.getCode())){
			request.setAttribute("rank", rank);
			request.setAttribute("rankIdError", "等级及语言代码必须为数字组成！");
			return mapping.findForward("toRankList");
		}
		if(rankf.getName() == null || "".equals(rankf.getName())){
			request.setAttribute("rank", rank);
			request.setAttribute("rankNameError", "请输入等级及语言！");
			
			return mapping.findForward("toRankList");
		}

		rank.setFlag(0);
		examService.saveRank(rank);

		return mapping.findForward("toRankList");
	}

	// 跳转到报考等级修改页面
	public ActionForward update(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		RankActionForm rankf = (RankActionForm) form;
		Rank rank =examService.findRankById(rankf.getId());
		request.setAttribute("rank", rank);
		
		return mapping.findForward("rankUpdate");
	}

	// 保存报考等级修改数据
	public ActionForward updateSave(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		RankActionForm rankf = (RankActionForm) form;
		
		// 判断当前输入的院系编号是否合法！
		if( ! rankf.getCode() .equals(examService.findRankById(rankf.getId()).getCode())){ //判断提交的院系编号是否被用户修改
	
		if (rankf.getCode() == null || "".equals(rankf.getCode())) {
			
			Rank rank = examService.findRankById(rankf.getId());
			request.setAttribute("rank", rank);
			
			request.setAttribute("rankIdError", "请输入等级及语言代码！");
			return mapping.findForward("rankUpdate");
		} 
		else if (examService.findRankByCode(rankf.getCode()) != null) {

			Rank rank = examService.findRankById(rankf.getId());
			request.setAttribute("rank", rank);
			
			request.setAttribute("rankIdRequest", "该等级及语言代码已存在，请重新输入！");

			return mapping.findForward("rankUpdate");
			}
		}
		if(!Pattern.matches("^[0-9]+$", rankf.getCode())){
			Rank rank = examService.findRankById(rankf.getId());
			request.setAttribute("rank", rank);
			request.setAttribute("rankIdError", "等级及语言代码必须为数字组成！");
			return mapping.findForward("toRankList");
		}
		if(rankf.getName() == null || "".equals(rankf.getName())){
			Rank rank = examService.findRankById(rankf.getId());
			request.setAttribute("rank", rank);
			
			request.setAttribute("rankNameError", "请输入等级及语言！");
			
			return mapping.findForward("rankUpdate");
		}
		
		Rank rank =examService.findRankById(rankf.getId());
		rank.setCode(rankf.getCode());
		rank.setName(rankf.getName());
		examService.updateRank(rank);

		return mapping.findForward("toRankList");
	}

	// 完成报考等级删除操作
	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		RankActionForm rankf = (RankActionForm) form;
		examService.deleteRankById(rankf.getId());

		return mapping.findForward("toRankList");
	}
}
